(function() {
    FlappyBird = {
        score: 0,
        isFirstClick: true, //是否是刚开始游戏
        isGameOver: false, //游戏是否结束
        Bird: {

            crateBird: function(size) {
                var bird = document.createElement("div");
                bird.setAttribute("id", "bird");
                bird.style.bottom = "100px";
                bird.style.left = size * 2 + "px";
                bird.style.width = size + "px";
                bird.style.height = size + "px";
                //                document.body.appendChild(bird);
                document.getElementById("display").appendChild(bird);
            },
            //上升动画，v为初速度，interval动画速度

            fly: function(v, interval) {
                if (arguments.length != 2) return; //参数不对则return
                var bird = document.getElementById("bird"); //获取bird元素
                var a = -9.8; //加速度
                var t = interval / 100;
                var v0 = v;
                var flyBtn = document.getElementById("flyBtn");
                var moveHandler;
                return function() {
                    moveHandler = setInterval(
                        function() {
                            var bottom = parseInt(bird.style.bottom);
                            if (bottom < 0) {
                                if (!FlappyBird.isGameOver)
                                    FlappyBird.showBoard(FlappyBird.score);
                                FlappyBird.isGameOver = true;
                                clearInterval(moveHandler);
                                return;
                            }
                            v0 += a * t; //速度
                            bottom += v0 * t;
                            bird.style.bottom = bottom + "px";
                        }, interval);

                    flyBtn.addEventListener("click", function() {
                        if (!FlappyBird.isFirstClick) {
                            clearInterval(moveHandler);
                        }
                    });
                    return moveHandler;
                }();

            }
        },
        Box: {
            boxWidth: 0,
            distance: 0,
            interval: 0,
            disHeight: 0,
            moveSpeed: 0,
            //参数分别为盒子宽度，开口高度，产生柱子的间隔，窗口高度
            createBoxs: function(boxWidth, distance, interval, disHeight) {
                var count = 0;
                var stackUp = [];
                var stackDown = [];
                var handler = setInterval(function() {
                    if (FlappyBird.isGameOver) {
                        clearInterval(handler);
                        return;
                    }
                    var upBox = document.createElement("div");
                    var downBox = document.createElement("div");
                    var display = document.getElementById("display");
                    var ranHeight = (disHeight - distance - 50) * Math.random() + 1;
                    upBox.setAttribute("class", "upBox");
                    upBox.style.width = boxWidth + "px";
                    upBox.style.right = -boxWidth + "px";
                    upBox.style.height = ranHeight + "px";
                    downBox.setAttribute("class", "downBox");
                    downBox.style.height = (disHeight - ranHeight - distance) + "px";
                    downBox.style.width = boxWidth + "px";
                    downBox.style.right = -boxWidth + "px";
                    downBox.style.top = ranHeight + distance + "px";
                    display.appendChild(upBox);
                    display.appendChild(downBox);
                    stackUp.push(upBox);
                    stackDown.push(downBox);
                    if (++count > 4) {
                        var temp = stackUp.shift();
                        temp.remove();
                        temp = null;
                        temp = stackDown.shift();
                        temp.remove();
                        temp = null;
                    }
                }, interval * 1000)
            },
            //盒子的移动函数
            move: function(speed) {
                //判断盒子和鸟是否撞上
                var upBoxs = document.getElementsByClassName("upBox");
                var downBoxs = document.getElementsByClassName("downBox");
                var shift;
                var bird = document.getElementById("bird");
                var handler = setInterval(function() {
                    if (FlappyBird.isGameOver) {
                        clearInterval(handler);
                        return;
                    }
                    for (var i = 0; i < upBoxs.length; i++) {
                        var downBox = downBoxs[i];
                        var upBox = upBoxs[i];
                        shift = parseInt(upBox.style.right);
                        upBox.style.right = shift + speed + "px";
                        shift = parseInt(downBox.style.right);
                        downBox.style.right = shift + speed + "px";
                        if (((bird.offsetTop <= upBox.offsetHeight) || (bird.offsetTop + bird.offsetHeight >= downBox.offsetTop)) && (bird.offsetLeft + bird.offsetWidth >= downBox.offsetLeft &&
                                bird.offsetLeft <= downBox.offsetLeft + downBox.offsetWidth)) {
                            FlappyBird.isGameOver = true;
                            FlappyBird.showBoard(FlappyBird.score);
                            clearInterval(handler);
                            return;
                        }

                        var difference = bird.offsetLeft - downBox.offsetLeft - downBox.offsetWidth;
                        if (difference <= speed && difference >= 0) {
                            FlappyBird.score += 1;
                            document.getElementById("score").innerHTML = FlappyBird.score + " points";
                        }
                    }
                }, 40);

            },
            //柱子宽度,开口程度，柱子产生间隔，游戏窗口高度，柱子移动速度
            init: function(boxWidth, difficulty, interval, disHeight, speed) {
                FlappyBird.Box.createBoxs(boxWidth, difficulty, interval, disHeight);
                FlappyBird.Box.move(speed);
            }

        },
        //创建一个div元素作为游戏窗口
        createDisplay: function(height, width) {
            var display = document.createElement("div");
            var score = document.createElement("p");
            score.setAttribute("id", "score");
            score.innerText = "0 points";

            display.setAttribute("id", "display");
            display.style.height = height + "px";
            display.style.width = width + "px";

            document.body.appendChild(score);
            document.body.appendChild(display);

        },
        //创建按钮
        createButtons: function() {
            //向上飞的按钮
            var fly = document.createElement("p");
            fly.innerHTML = "FLY";
            //            fly.setAttribute("class", "button");
            fly.setAttribute("id", "flyBtn");

            //游戏开始按钮
            var startBtn = document.createElement("a");
            startBtn.innerHTML = "START";
            startBtn.setAttribute("id", "startGame");

            //从新开始游戏按钮
            var restartBtn = document.createElement("a");
            restartBtn.innerHTML = "RESTART";
            restartBtn.setAttribute("id", "restart");

            document.body.appendChild(startBtn);
            document.body.appendChild(restartBtn);
            document.body.appendChild(fly);
        },
        //创建显示结果的遮罩层
        createBoard: function() {
            var display = document.getElementById("display");
            var width = display.style.width;
            var height = display.style.height;
            var board = document.createElement("div");
            board.setAttribute("id", "board");
            board.style.width = width;
            board.style.height = height;
            board.style.lineHeight = height;
            display.appendChild(board);
        },
        //显示遮罩层
        showBoard: function(score) {
            var board = document.getElementById("board");
            board.innerText = "You get " + score + " points, suck!";
            board.style.visibility = "visible";
        },
        //隐藏遮罩层
        hideBoard: function() {
            var board = document.getElementById("board");
            board.style.visibility = "hidden";
        },
        pause: function() {

        },
        //游戏初始化
        gameInit: function() {
            var viewHeight = window.innerHeight;
            var viewWidth = window.innerWidth;
            FlappyBird.createDisplay(viewHeight * 0.6, viewWidth);
            FlappyBird.createButtons();
            FlappyBird.createBoard(viewHeight, viewWidth);
            //开始游戏事件
            document.getElementById("startGame").addEventListener("click", function() {
                if (FlappyBird.isFirstClick && !FlappyBird.isGameOver) {
                    FlappyBird.Bird.crateBird(innerHeight * 0.022);
                    FlappyBird.Bird.fly(viewHeight * 0.065, 7);
                    FlappyBird.Box.init(viewWidth * 0.12, viewHeight * 0.2, 1.2, viewHeight * 0.6, viewWidth * 0.025);
                    FlappyBird.isFirstClick = false;
                }
            });
            //按钮向上飞的事件
            document.getElementById("flyBtn").onclick = function() {
                FlappyBird.Bird.fly(viewHeight * 0.071, 8);

            };
            //重置游戏事件
            document.getElementById("restart").onclick = function() {
                var board = document.getElementById("board");
                var display = document.getElementById("display");
                display.innerHTML = "";
                display.appendChild(board);
                FlappyBird.isFirstClick = true;
                FlappyBird.isGameOver = false;
                FlappyBird.score = 0;
                document.getElementById("score").innerHTML = FlappyBird.score + " points";
                FlappyBird.hideBoard();
            }
        }
    };

}());